001 /* EVolve - an Extensible Software Visualization Framework 002 * Copyright (C) 2001-2002 Qin Wang 003 * 004 * This library is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Library General Public 006 * License as published by the Free Software Foundation; either 007 * version 2 of the License, or (at your option) any later version. 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Library General Public License for more details. 013 * 014 * You should have received a copy of the GNU Library General Public 015 * License along with this library; if not, write to the 016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 017 * Boston, MA 02111-1307, USA. 018 */ 019 020 /* 021 * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/ 022 */ 023 024 package EVolve.data; 025 026 import EVolve.*; 027 import EVolve.exceptions.DataProcessingException; 028 import EVolve.exceptions.EVolveException; 029 import EVolve.visualization.*; 030 import java.awt.*; 031 import java.util.*; 032 import java.io.File; 033 034 public class DataManager implements Worker{ 035 private DataSource dataSource; 036 private ElementDefinition[] definition; 037 private ReferenceLink[] link; 038 private HashMap[] entity; 039 private ElementFilter[][] elementFilter; 040 private Color color; 041 private int colorRGB; 042 private long eventCounter; 043 private long[] eventInterval; 044 private int currentInterval; 045 private boolean dataProcessed; 046 private boolean dataLoaded; 047 private int state; 048 049 050 public DataManager(DataSource dataSource) { 051 this.dataSource = dataSource; 052 this.eventCounter = 0; 053 dataLoaded = false; 054 } 055 056 public void init() throws EVolveException { 057 ElementBuilder.init(); 058 059 dataSource.init(); 060 061 dataSource.startBuildDefinition(); 062 063 ArrayList definitionList = new ArrayList(); 064 ElementDefinition nextDefinition = dataSource.getNextDefinition(); 065 while (nextDefinition != null) { 066 definitionList.add(nextDefinition); 067 nextDefinition = dataSource.getNextDefinition(); 068 } 069 070 definition = new ElementDefinition[definitionList.size()]; 071 for (int i = 0; i < definition.length; i++) { 072 definition[((ElementDefinition)(definitionList.get(i))).getType()] = (ElementDefinition)(definitionList.get(i)); 073 } 074 075 /*ArrayList[] entityList = new ArrayList[definition.length]; 076 for (int i = 0; i < entityList.length; i++) { 077 entityList[i] = new ArrayList(); 078 }*/ 079 080 entity = new HashMap[definition.length]; 081 for (int i = 0; i < entity.length; i++) { 082 entity[i] = new HashMap(); 083 } 084 085 dataSource.startBuildEntity(); 086 087 /*Entity nextEntity = dataSource.getNextEntity(); 088 while (nextEntity != null) { 089 entityList[nextEntity.getType()].add(nextEntity); 090 nextEntity = dataSource.getNextEntity(); 091 }*/ 092 093 Entity nextEntity = dataSource.getNextEntity(); 094 while (nextEntity != null) { 095 int type = nextEntity.getType(); 096 entity[type].put(new Long(nextEntity.getId()),nextEntity); 097 nextEntity = dataSource.getNextEntity(); 098 } 099 100 /*entity = new Entity[entityList.length][]; 101 102 for (int i = 0; i < entity.length; i++) { 103 entity[i] = new Entity[entityList[i].size()]; 104 for (int j = 0; j < entity[i].length; j++) { 105 entity[i][((Entity)(entityList[i].get(j))).getId()] = (Entity)(entityList[i].get(j)); 106 } 107 }*/ 108 109 // creates links 110 ArrayList list = new ArrayList(); 111 for (int i = 0; i < definition.length; i++) { 112 for (int j = 0; j < definition[i].getFieldDefinition().length; j++) { 113 if ((definition[i].getFieldDefinition()[j].getReference() != -1) && 114 (definition[i].getFieldDefinition()[j].getReference() != i)) { 115 list.add(new ReferenceLink(definition[i].getFieldDefinition()[j].getName(), i, 116 j, definition[i].getFieldDefinition()[j].getReference(), 117 definition[i].getFieldDefinition()[j].getDescription(), 118 definition[i].getFieldDefinition()[j].getProperties())); 119 } 120 } 121 } 122 123 int head = 0; 124 int tail = list.size(); 125 int initSize = list.size(); 126 127 while (head < tail) { 128 for (int i = head; i < tail; i++) { 129 for (int j = 0; j < initSize; j++) { 130 ReferenceLink from = (ReferenceLink)(list.get(j)); 131 ReferenceLink to = (ReferenceLink)(list.get(i)); 132 if ((from.getTargetType() == to.getSourceType()) && (from.getSourceType() != to.getTargetType())) { 133 list.add(new ReferenceLink(from, to)); 134 } 135 } 136 } 137 head = tail; 138 tail = list.size(); 139 } 140 141 link = new ReferenceLink[list.size()]; 142 for (int i = 0; i < link.length; i++) { 143 link[i] = (ReferenceLink)(list.get(i)); 144 } 145 } 146 147 public void sendEvents() throws DataProcessingException { 148 149 state = Worker.STATE_RUNNING; 150 151 Selection[] selection = Scene.getFilter().getSelection(); 152 Selection activeSelection = Scene.getFilter().getActiveSelection(); 153 dataProcessed = false; 154 if (activeSelection != null) { 155 eventInterval = new long[4]; 156 eventInterval[0] = 0; 157 eventInterval[1] = activeSelection.getStart(); 158 eventInterval[2] = activeSelection.getEnd(); 159 eventInterval[3] = Integer.MAX_VALUE; 160 } else { 161 eventInterval = new long[2]; 162 eventInterval[0] = 0; 163 eventInterval[1] = Integer.MAX_VALUE; 164 } 165 166 elementFilter = new ElementFilter[eventInterval.length - 1][definition.length]; 167 for (int i = 0; i < elementFilter.length; i++) { 168 boolean isIncluded = activeSelection == null ? true : false; 169 for (int j = 0; j < selection.length; j++) { 170 if ((selection[j] == activeSelection)&&(selection[j].getStart() >= eventInterval[i])&&(selection[j].getEnd() <= eventInterval[i+1])) { 171 isIncluded = true; 172 break; 173 } 174 } 175 176 if (isIncluded) 177 { 178 ArrayList list = new ArrayList(); 179 if (activeSelection != null) list.add(activeSelection); 180 for (int j = 0; j < selection.length; j++) { 181 if ((selection[j].getColor()!=null) && (selection[j] != activeSelection)) { 182 list.add(selection[j].specialClone()); 183 } 184 } 185 Selection[] tempSelection = new Selection[list.size()]; 186 for (int j = 0; j < tempSelection.length; j++) { 187 tempSelection[j] = (Selection)(list.get(j)); 188 } 189 190 for (int j = 0; j < definition.length; j++) { 191 if (Scene.getVisualizationManager().getVisualizationType().indexOf("" + j + "") != -1) { 192 elementFilter[i][j] = new ElementFilter(definition[j], tempSelection); 193 } else { 194 elementFilter[i][j] = null; 195 } 196 } 197 } else { 198 elementFilter[i] = null; 199 } 200 201 } 202 203 currentInterval = 0; 204 205 dataSource.startBuildEvent(); 206 eventCounter = 0; 207 208 Event nextEvent = dataSource.getNextEvent(); 209 while (nextEvent != null) { 210 211 if (state == STATE_PAUSING) { 212 try { 213 synchronized(this) { 214 this.state = STATE_PAUSED; 215 wait(); 216 } 217 state = STATE_RUNNING; 218 } catch (InterruptedException e) { 219 if (state == STATE_STOPPING) { 220 state = STATE_STOPPED; 221 throw new DataProcessingException("Data processing cancelled by the user."); 222 } 223 } 224 } else if (state == STATE_STOPPING) { 225 state = STATE_STOPPED; 226 throw new DataProcessingException("Data processing cancelled by the user."); 227 } 228 229 230 if ((elementFilter[currentInterval] != null) && (elementFilter[currentInterval][nextEvent.getType()] != null)) { 231 color = elementFilter[currentInterval][nextEvent.getType()].getColor(nextEvent); 232 if (color != null) { 233 colorRGB = color.getRGB(); 234 Scene.getVisualizationManager().receiveEvent(nextEvent); 235 } 236 } 237 238 if (eventCounter == eventInterval[currentInterval + 1]) { 239 currentInterval++; 240 } 241 242 eventCounter++; 243 nextEvent = dataSource.getNextEvent(); 244 } 245 dataProcessed = true; 246 state = STATE_FINISHED; 247 } 248 249 public HashMap[] getEntity() { 250 return entity; 251 } 252 253 public ElementDefinition[] getElementDefinition() { 254 return definition; 255 } 256 257 public ElementDefinition[] getElementDefinition(VisualizationDefinition visualizationDefinition) { 258 ArrayList list = new ArrayList(); 259 for (int i = 0; i < definition.length; i++) { 260 boolean add = true; 261 for (int j = 0; j < visualizationDefinition.getDimensionDefinition().length; j++) { 262 DataFilter[] temp = getDataFilter(definition[i], visualizationDefinition.getDimensionDefinition()[j].getProperty()); 263 if (temp.length == 0) { 264 add = false; 265 break; 266 } 267 } 268 if (add) { 269 list.add(definition[i]); 270 } 271 } 272 273 ElementDefinition[] returnVal = new ElementDefinition[list.size()]; 274 for (int i = 0; i < returnVal.length; i++) { 275 returnVal[i] = (ElementDefinition)(list.get(i)); 276 } 277 278 return returnVal; 279 } 280 281 public DataFilter[] getDataFilter(ElementDefinition subjectDefinition, String property) { 282 ArrayList list = new ArrayList(); 283 FieldDefinition fd; 284 boolean nonLinkField = true; 285 286 for (int i = 0; i<link.length; i++) { 287 if (link[i].hasProperty(property)) { 288 if (link[i].getSourceType() == subjectDefinition.getType()) { 289 list.add(new DataFilter(link[i].getName(), link[i].getSourceIndex(), link[i], link[i].getDescription(),null)); 290 } 291 nonLinkField = false; 292 } 293 } 294 295 if (nonLinkField) { 296 for (int i = 0; i < subjectDefinition.getFieldDefinition().length; i++) { 297 fd = subjectDefinition.getFieldDefinition()[i]; 298 if (subjectDefinition.getFieldDefinition()[i].hasProperty(property)) { 299 list.add(new DataFilter(subjectDefinition.getFieldDefinition()[i].getName(), i, 300 null, subjectDefinition.getFieldDefinition()[i].getDescription(),fd.getProperties())); 301 } 302 } 303 } 304 305 DataFilter[] returnVal = new DataFilter[list.size()]; 306 for (int i = 0; i < returnVal.length; i++) { 307 returnVal[i] = (DataFilter)(list.get(i)); 308 } 309 310 return returnVal; 311 } 312 313 public Color getColor() { 314 return color; 315 } 316 317 public int getColorRGB() { 318 return colorRGB; 319 } 320 321 public long getEventCounter() { 322 return eventCounter; 323 } 324 325 public ReferenceLink[] getReferenceLink() { 326 return link; 327 } 328 329 public boolean isCompleted() { 330 return dataProcessed; 331 } 332 333 public String getDatasourceName() { 334 String fileName = dataSource.getFileName(); 335 336 if (fileName == null) 337 fileName = "No Data"; 338 else 339 fileName = fileName.substring(fileName.lastIndexOf(File.separatorChar)+1); 340 341 return dataSource.getName()+" - "+fileName; 342 } 343 344 public boolean isDataLoaded() { 345 return dataLoaded; 346 } 347 348 public void setDataLoaded(boolean loaded) { 349 dataLoaded = loaded; 350 } 351 352 public DataSource getDataSource() { 353 return dataSource; 354 } 355 356 public synchronized void resume() { 357 notifyAll(); 358 } 359 360 public synchronized void pause() { 361 if (state == STATE_RUNNING) { 362 state = STATE_PAUSING; 363 } 364 } 365 366 public synchronized void stop() { 367 switch (state) { 368 case STATE_NOT_STARTED: 369 case STATE_STOPPED: 370 case STATE_FINISHED: 371 break; 372 default: 373 state = STATE_STOPPING; 374 break; 375 } 376 } 377 378 public synchronized long getCurrentProgress() { 379 return eventCounter; 380 } 381 382 public synchronized long getMaxProgress() { 383 return dataSource.getTotalNumberOfEvents(); 384 //return 100; 385 } 386 387 public synchronized int getCurrentState() { 388 return state; 389 } 390 391 public synchronized void start() { 392 state = STATE_RUNNING; 393 } 394 395 public void join() throws InterruptedException { 396 } 397 398 public void resetState() { 399 state = Worker.STATE_NOT_STARTED; 400 } 401 }